package edu.ucsc.gameAI;

import java.awt.Color;

import pacman.game.Constants.DM;
import pacman.game.Constants.GHOST;
import pacman.game.Constants.MOVE;
import pacman.game.Game;
import pacman.game.GameView;
import edu.ucsc.gameAI.conditions.AreTherePowerPillsLeft;
import edu.ucsc.gameAI.decisionTrees.binary.IBinaryNode;
import edu.ucsc.gameAI.fsm.IState;

public class EatPills implements IAction, IBinaryNode {

	double safeDistance = PacmanConstants.safeDistance;
	//int euclidianSafeDistance = 20;


	@Override
	public void doAction() {
		// TODO Auto-generated method stub

	}

	@Override
	public MOVE getMove() {
		return null;
	}

	public MOVE getMove(Game game, IState state) {
		//System.out.println("Inside of Eat Pills.java!");
		int pacmanIndex = game.getPacmanCurrentNodeIndex();
		int closestPill = game.getClosestNodeIndexFromNodeIndex(pacmanIndex,
				game.getActivePillsIndices(), DM.PATH);
		double currentDistance;
		double bestDistance = Double.MAX_VALUE;
		boolean isPowerPillInTheWay = false;
		boolean isGhostInTheWay = false;

		// Loop through all available regular pills, to find the next one to eat.
		// The next one to eat should ideally NOT make Pacman cross through a
		// power pellet.
		// This is because power pellets should be saved for when there are
		// ghosts nearby to be eaten!
		for (int pillIndex : game.getActivePillsIndices()) {
			isPowerPillInTheWay = false;
			isGhostInTheWay = false;
			// pillIndex represents a regular pill. Let's grab the shortest path
			// to that pill.
			int[] pathToThisPillIndex = game.getShortestPath(pacmanIndex,
					pillIndex);

			// Great, now we have the shortest path, let's make sure that it
			// doesn't include a power pill.
			// AND... I think maybe we can save ourselves a little bit of
			// overhead by only caring about the power pill CLOSEST to pacman.
			int closestPowerPillIndex = game.getClosestNodeIndexFromNodeIndex(
					pacmanIndex, game.getActivePowerPillsIndices(), DM.PATH);
			for (int i = 0; i < pathToThisPillIndex.length; i++) {
				if (pathToThisPillIndex[i] == closestPowerPillIndex) {
					// Oh oh, there is a power pill on this path. DON'T EAT THIS
					// PILL!!!!
					isPowerPillInTheWay = true;
					break;
				}
			}
			if (isPowerPillInTheWay) {
				// we aren't interested in this pill because pacman would have
				// to eat a power pellet. let's move on to the next one.
				// Maybe in the future we could do something smarter, like
				// finding an alternative path.
				continue;
			}
			
			//And, you know what, let's not eat a pill if there is a ghost on this path, either, huh?
			for(GHOST ghostType: GHOST.values()){
				if(game.isGhostEdible(ghostType))
					continue;
				int ghostIndex = game.getGhostCurrentNodeIndex(ghostType);
				for(int i = 0; i < pathToThisPillIndex.length; i++){
					if((pathToThisPillIndex[i] == ghostIndex && game.getShortestPathDistance(pacmanIndex, ghostIndex) < safeDistance)) {
						//Yikes! This path has a ghost on it! Let's eat a different pill, shall we?
						isGhostInTheWay = true;
					}
				}
			}
			
			if(isGhostInTheWay){
				//We aren't intersted in this pill because pacman would have to move through a ghost.
				//Let's move on to the next one.
				//maybe in the future we could do something smarter, like find an alternative path.
				continue;
			}
			
			currentDistance = game.getDistance(pacmanIndex, pillIndex, DM.PATH);

			if (currentDistance < bestDistance) { // &&
													// game.getDistance(pillIndex,
													// closestPowerPillIndex,
													// DM.PATH) >=
													// powerPillDistance) {
				bestDistance = currentDistance;
				closestPill = pillIndex;
			}

		}

		if (closestPill != -1) {
			
			int[] closestPillPath = game.getShortestPath(pacmanIndex, closestPill);
			GameView.addPoints(game, Color.BLUE, closestPillPath );
			GameView.addPoints(game, Color.RED, closestPill);
			
			return game.getNextMoveTowardsTarget(pacmanIndex, closestPill,
					DM.PATH);
		} else if (new AreTherePowerPillsLeft().test(game)){
			int closestPP = game.getClosestNodeIndexFromNodeIndex(pacmanIndex,
					game.getActivePowerPillsIndices(), DM.PATH);

			for (int pillIndex : game.getActivePillsIndices()) {
				currentDistance = game.getDistance(pacmanIndex, pillIndex,
						DM.PATH);

				if (currentDistance < bestDistance) {
					bestDistance = currentDistance;
					closestPP = pillIndex;
				}
			}
			return game.getNextMoveTowardsTarget(pacmanIndex, closestPP,
					DM.PATH);
		}
		else {
			Debug.debug("--->ACTUALLY doing runAwayFromGhosts", Debug.MESSAGETYPE.STATEMENT);
			return new RunAwayFromGhosts().getMove(game, state);
		}
	}

	@Override
	public IAction makeDecision(Game game) {
		return null;
	}
	
	@Override
	public String toString(){
		return "EatPillsAction";
	}

}
